[% setvar title Operators: Polymorphic comparisons %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>Operators: Polymorphic comparisons</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Maintainer: Damian Conway &lt;<a href='mailto:damian@conway.org'>damian@conway.org</a>&gt;
  Date: 7 Aug 2000
  Last Modified: 18 Sep 2000
  Mailing List: <a href='mailto:perl6-language@perl.org'>perl6-language@perl.org</a>
  Number: 54
  Version: 2
  Status: Frozen</pre>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>This RFC proposes that numeric comparison operators default to stringwise
comparison when both arguments are non-numeric strings.</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<p>Currently the expression:</p>
<pre>	&quot;cat&quot; == &quot;dog&quot;</pre>
<p>returns true.</p>
<p>It is proposed that if <i>neither</i> argument of a numeric comparison
operator can be converted to a number, rather than both being converted
to zero, the two operands should be compared using the equivalent
stringwise comparison operator.</p>
<p>It is further proposed that the current warning:</p>
<pre>        Argument &quot;%s&quot; isn't numeric</pre>
<p>be changed to:</p>
<pre>        Arguments of &quot;%s&quot; aren't numeric - using string comparison instead</pre>
<a name='RATIONALE'></a><h1>RATIONALE</h1>
<p>Perl has excellent support for generic programming, because of its dynamic
typing, generic data types, and interface polymorphism. Just about the only
place where that DWIM genericity breaks down is in comparisons.</p>
<p>It is difficult to construct many generic algorithms and data structures
in Perl, because there is no generic way to specify an ordering on
dynamically typed data. For example, one cannot simply code a
generic BST insertion method:</p>
<pre>        sub insert
        {
                my ($self, $key, $value) = @_;
                if (!defined($self-&gt;{key}))
                {
                        $self-&gt;{key} = $key;
                        return $self-&gt;{value} = $value;
                }
                my $compare = $key &lt;=&gt; $self-&gt;{key};
                return $self-&gt;{value} = $value unless $compare;

                $self-&gt;{$compare} = $self-&gt;new() unless $self-&gt;{$compare};
                return $self-&gt;{$compare}-&gt;insert($key,$value);
        }</pre>
<p>This will work well for numeric keys, but fail miserably on most
string-based keys, because &lt;=&gt; will generally return 0 for most pairs of
strings.</p>
<p>The above code would work correctly however if &lt;=&gt; detected the string/string
comparison and automagically used cmp instead.</p>
<a name='IMPLEMENTATION'></a><h1>IMPLEMENTATION</h1>
<p>Dammit, Jim, I'm a doctor, not an engineer!</p>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<p>Chapter 12 of &quot;Object Oriented Perl&quot;</p>
</div>
